
tpFuncion = '';

dynamicGridUi = Ext.extend(Ext.grid.GridPanel, {

	viewConfig: {
        forceFit: true
	},
	region: 'center',
	autoHeight: false,
	height: 460,
	id: 'dynaGrid',
	bodyStyle: 'borderStyle: none',
	store: data = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy(
			dataConnection = new Ext.data.Connection({
				url:'/agvWeb/report.do?method=getReport&tpReport=',
				listeners: {
			   		'beforerequest': function () {
			   			dataConnection.url =  dataConnection.url + document.getElementById('tpReport').value;
			   		}
				}
			})
		),
		reader:new Ext.data.JsonReader({
							
		})
	}),
	sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
	tbar    : toolbar =  new Ext.Toolbar({
		buttonAlign: 'right',
    	items   : [         
        	{xtype: 'tbseparator'},
        	{text : "&nbsp;<b>exportar a pdf</b>",handler: function() {ventana(); },iconCls:'silk-pdf'},
        	{xtype: 'tbseparator'},
        	{
        		text:"&nbsp;<b>exportar a excel</b>", 
        		scope:this, 
        		handler: function(e,o) {
					Ext.Ajax.request({
					         url : '/agvWeb/report.do?method=getXlsReport',
					                  method: 'POST',
					                  success: function ( result, request ) {
					               	  				alert('success');	
					               	  			},
					                  failure: function ( result, request ) {
					               	  				alert('fail');	
					               	  			}
					       });
        		},
        		iconCls:'silk-excel'
        	},
        	{
        		xtype: 'tbseparator'
        	},
        	{
        		text:"&nbsp;<b>exportar a texto</b>", 
        		scope:this, 
        		handler: function(e,o) {
					Ext.Ajax.request({
					         url : '/agvWeb/report.do?method=getTxtReport',
					                  method: 'POST',
					                  success: function ( result, request ) {
					               	  				
					               	  				Ext.Ajax.request({
					         url : '/agvWeb/test.txt',
					                  method: 'POST',
					                  success: function ( result, request ) {
					               	  				alert('success');	
					               	  			},
					                  failure: function ( result, request ) {
					               	  				alert('fail');	
					               	  			}
					       });
					               	  				
					               	  					
					               	  			},
					                  failure: function ( result, request ) {
					               	  				alert('fail');	
					               	  			}
					       });
        		},
        		iconCls:'silk-table'
        	},
        	{xtype: 'tbseparator'},
        	{
        		text:"&nbsp;<b>imprimir</b>", 
        		scope:this, 
				handler: function(e,o) {
					var grid = Ext.getCmp('dynaGrid'); 
					Ext.ux.GridPrinter.print(grid); 
				},        		
				iconCls:'silk-printer'},
        	{xtype: 'tbseparator'}
    	]
	}),
		cm: gridColModel = new Ext.grid.ColumnModel({
					columns: columns = []
				}),
		title: 'Reporte',
		frame:true,
		border: false,
		bbar:new Ext.PagingToolbar({
			//pageSize:10,
			store:data,
			displayInfo:true
			//displayMsg: 'Mostrando {0} - {1} de {2}',
			//emptyMsg:'sin Datos'
		}),
		listeners: {
		
	   		'render': function (comp) {

				var index = 0;

				var jsonstore = new Ext.data.Store({
					proxy: new Ext.data.HttpProxy(
						new Ext.data.Connection({
							url:'/agvWeb/report.do?method=getReportConf&tpReport=' + document.getElementById('tpReport').value
						})
					),
					reader:new Ext.data.JsonReader({
						root:'rows',
						totalCount: 'total',
						fields:['header','align','sortable','width','dataIndex']
					}),
					listeners: {
					
				   		'load': function () {
							jsonstore.each(function(r) {
								columns[index] = { 
													header: '<b>' + r.data['header'] +  '</b>',
												   	align: r.data['align'], 
												   	sortable: r.data['sortable'],
												   	//width: r.data['width'],
												   	dataIndex: r.data['dataIndex']};
						        index ++;
						   	});
						    gridColModel.setConfig(columns);
				   		},
						exception: function(proxy, type, action, options, response, arg) {
									        Ext.MessageBox.show({
									           title:'AGV Software',
									           style: 'vertical-align: middle;',
									           msg: '<b>El reporte que intenta visualizar, no ha sido configurado a&uacute;n</b>',
									           buttons: Ext.MessageBox.OK,
									           icon: Ext.MessageBox.ERROR
									       });
										} 				   		
				  }
				  
				});
				
				jsonstore.load();
    			data.load({params: {meta: true, start: 0}});
	   		}
		},

    initComponent: function(arguments) {
    	document.getElementById('tpReport').value = this.tpFuncion;
    	//alert(document.getElementById('tpCatalogTxt').value);
        dynamicGridUi.superclass.initComponent.call(this);
    }

});

	dynamicGrid = Ext.extend(dynamicGridUi, {
	    initComponent: function(arguments) {
	        dynamicGrid.superclass.initComponent.call(this);
	    }
	});


